package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@Transactional

public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    //增加套餐
    @Transactional
    @Override
    public void addSetmeal(SetmealVO setmealVO) {
        log.info("增加套餐操作");
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
         setmealMapper.addSetmeal(setmeal);
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        //增加菜品关系

        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        for(SetmealDish setmealDish : setmealDishes){
            System.out.println(setmealDish.getSetmealId());
        }
        setmealDishMapper.addSetmealDish(setmealDishes);

    }

    @Override
    public PageResult selectPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.selectPage(setmealPageQueryDTO) ;
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void setStatus(Long status, Long id) {
        setmealMapper.setStatus(status,id);
    }
    @Transactional
    @Override
    public void deleteSetmeals(List<Long> ids)  {
        if(ids.size() == 0){
            throw new DeletionNotAllowedException("批量操作数据为零");
        }
        for(Long id : ids){
            if(setmealMapper.selectIdStatus(id) == StatusConstant.ENABLE)
                throw new DeletionNotAllowedException("起售中");
        }
        setmealDishMapper.deleteSetmeal(ids);
        setmealMapper.deleteSetmeal(ids);
    }

    @Override
    public SetmealDTO seleteById(Long id) {
        SetmealDTO setmealDTO = new SetmealDTO();
        Setmeal setmeal = setmealMapper.seleteSetmeal(id);
        BeanUtils.copyProperties(setmeal,setmealDTO);
        List<SetmealDish> setmealDishes = setmealDishMapper.seleteSetmeal(id);
        setmealDTO.setSetmealDishes(setmealDishes);
        return setmealDTO;
    }

    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        setmealDishMapper.deleteSD(setmealDTO.getId());
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        list.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
        });
        setmealDishMapper.addSetmealDish(list);
    }

}
